package com.td.module.equipment.service.acquisitioninfo;

import com.td.framework.common.pojo.PageResult;
import com.td.framework.tenant.core.aop.TenantIgnore;
import com.td.module.equipment.controller.admin.acquisitioninfo.vo.AcquisitionInfoCreateReqVO;
import com.td.module.equipment.controller.admin.acquisitioninfo.vo.AcquisitionInfoExportReqVO;
import com.td.module.equipment.controller.admin.acquisitioninfo.vo.AcquisitionInfoPageReqVO;
import com.td.module.equipment.controller.admin.acquisitioninfo.vo.AcquisitionInfoUpdateReqVO;
import com.td.module.equipment.convert.acquisitioninfo.AcquisitionInfoConvert;
import com.td.module.equipment.dal.dataobject.acquisitioninfo.AcquisitionInfoDO;
import com.td.module.equipment.dal.mysql.acquisitioninfo.AcquisitionInfoMapper;
import com.td.module.equipment.framework.core.dto.EquipmentDataDto;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.td.module.equipment.enums.ErrorCodeConstants.ACQUISITION_INFO_NOT_EXISTS;

/**
 * 设备采集信息 Service 实现类
 *
 * @author 王翔翔
 */
@Service
@Validated
public class AcquisitionInfoServiceImpl implements AcquisitionInfoService {

    @Resource
    private AcquisitionInfoMapper acquisitionInfoMapper;

    @Override
    public Long createAcquisitionInfo(AcquisitionInfoCreateReqVO createReqVO) {
        // 插入
        AcquisitionInfoDO acquisitionInfo = AcquisitionInfoConvert.INSTANCE.convert(createReqVO);
        acquisitionInfoMapper.insert(acquisitionInfo);
        // 返回
        return acquisitionInfo.getId();
    }

    @Override
    public void updateAcquisitionInfo(AcquisitionInfoUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateAcquisitionInfoExists(updateReqVO.getId());
        // 更新
        AcquisitionInfoDO updateObj = AcquisitionInfoConvert.INSTANCE.convert(updateReqVO);
        acquisitionInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteAcquisitionInfo(Long id) {
        // 校验存在
        this.validateAcquisitionInfoExists(id);
        // 删除
        acquisitionInfoMapper.deleteById(id);
    }

    private void validateAcquisitionInfoExists(Long id) {
        if (acquisitionInfoMapper.selectById(id) == null) {
            throw exception(ACQUISITION_INFO_NOT_EXISTS);
        }
    }

    @Override
    public AcquisitionInfoDO getAcquisitionInfo(Long id) {
        return acquisitionInfoMapper.selectById(id);
    }

    @Override
    public List<AcquisitionInfoDO> getAcquisitionInfoList(Collection<Long> ids) {
        return acquisitionInfoMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<AcquisitionInfoDO> getAcquisitionInfoPage(AcquisitionInfoPageReqVO pageReqVO) {
        return acquisitionInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AcquisitionInfoDO> getAcquisitionInfoList(AcquisitionInfoExportReqVO exportReqVO) {
        return acquisitionInfoMapper.selectList(exportReqVO);
    }

    @Override
    public void createAcquisitionInfo(EquipmentDataDto equipmentDataDto) {

        List<AcquisitionInfoDO> acquisitionInfoDOList = AcquisitionInfoConvert.INSTANCE.converList03(equipmentDataDto);
        acquisitionInfoMapper.insertBatch(acquisitionInfoDOList);

    }

    @TenantIgnore
    @Override
    public List<AcquisitionInfoDO> getInfoGroupMonitorTime(AcquisitionInfoPageReqVO pageReqVO) {
        return acquisitionInfoMapper.selectGroupMonitorTime(pageReqVO);
    }

    @Override
    public Integer sumWaterQuality() {
        return acquisitionInfoMapper.sumWaterQuality();
    }

}
